import userApi from '@/api/user';
export default {
    state: {
        userinfo: JSON.parse(localStorage.getItem('userinfo')) || {} ,  //用户的信息
        token:localStorage.getItem('token') || ''
    },
    mutations: {
        SET_USERINFO(state,userinfo){
            state.userinfo = userinfo;
        },
        SET_TOKEN(state,token){
            state.token = token;
        }
    },
    actions: {
        async loginAction({commit,dispatch},{phone,checkcode}){
            let result = await userApi.login(phone,checkcode)
            if(result.data.code === 1){
                let {token} = result.data.data;

                commit('SET_TOKEN',token);
                localStorage.setItem('token',token);
                
                //获取个人信息

                dispatch('getUserInfo')
            }
            return result
        },
        async getUserInfo({commit}){
            let result = await userApi.getUserInfo();
            if(result.data.code === 1){
                commit('SET_USERINFO',result.data.data);
                localStorage.setItem('userinfo',JSON.stringify(result.data.data))
            }
        }
    },
    namespaced:true
}